package com.ttmall.rest.controller;

import com.ttmall.common.pojo.ItemCatResult;
import com.ttmall.common.utils.JsonUtils;
import com.ttmall.rest.service.ItemCatService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by GalaIO on 2018/1/16.
 */
@RestController
@RequestMapping("/item/cat")
public class ItemCatController {

    @Autowired
    private ItemCatService itemCatService;

    @RequestMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    public String listItemCat(@RequestParam(required = false) String callback) {
        ItemCatResult result = itemCatService.listItemCat();
        String json = JsonUtils.objectToJson(result);
        if (StringUtils.isEmpty(callback)) {
            return json;
        }
        //如果字符串不为孔 转换为jsonp调用
        return callback + "(" + json + ");";
    }

}
